package com.dy.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.dy.common.utils.StringUtils;
import com.dy.system.domain.AppletBanner;
import com.dy.system.service.IAppletBannerService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.dy.common.annotation.Log;
import com.dy.common.core.controller.BaseController;
import com.dy.common.core.domain.AjaxResult;
import com.dy.common.enums.BusinessType;
import com.dy.common.utils.poi.ExcelUtil;
import com.dy.common.core.page.TableDataInfo;

/**
 * 小程序bannerController
 *
 * @author ruoyi
 * @date 2022-08-19
 */
@RestController
@RequestMapping("/system/banner")
public class AppletBannerController extends BaseController {
    @Autowired
    private IAppletBannerService appletBannerService;

    /**
     * 查询小程序banner列表
     */
    @PreAuthorize("@ss.hasPermi('system:banner:list')")
    @GetMapping("/list")
    public TableDataInfo list(AppletBanner appletBanner)
    {
        startPage();
        List<AppletBanner> list = appletBannerService.selectAppletBannerList(appletBanner);
        String imgUrl = "https://dyep.cc/prod-api";
        list.forEach(m ->{
            m.setNoticeCover(StringUtils.isNoneBlank(m.getNoticeCover()) ? imgUrl + m.getNoticeCover() : "");
        });
        return getDataTable(list);
    }

    /**
     * 导出小程序banner列表
     */
    @PreAuthorize("@ss.hasPermi('system:banner:export')")
    @Log(title = "小程序banner", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AppletBanner appletBanner)
    {
        List<AppletBanner> list = appletBannerService.selectAppletBannerList(appletBanner);
        ExcelUtil<AppletBanner> util = new ExcelUtil<AppletBanner>(AppletBanner.class);
        util.exportExcel(response, list, "小程序banner数据");
    }

    /**
     * 获取小程序banner详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:banner:query')")
    @GetMapping(value = "/{bannerId}")
    public AjaxResult getInfo(@PathVariable("bannerId") Long bannerId)
    {
        AppletBanner banner = appletBannerService.selectAppletBannerByBannerId(bannerId);
        if (banner != null){
            String imgUrl = "https://dyep.cc/prod-api";
            banner.setNoticeCover(StringUtils.isNoneBlank(banner.getNoticeCover()) ? imgUrl + banner.getNoticeCover() : "");
        }
        return AjaxResult.success(banner);
    }

    /**
     * 新增小程序banner
     */
    @PreAuthorize("@ss.hasPermi('system:banner:add')")
    @Log(title = "小程序banner", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AppletBanner appletBanner)
    {
        return toAjax(appletBannerService.insertAppletBanner(appletBanner));
    }

    /**
     * 修改小程序banner
     */
    @PreAuthorize("@ss.hasPermi('system:banner:edit')")
    @Log(title = "小程序banner", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AppletBanner appletBanner)
    {
        return toAjax(appletBannerService.updateAppletBanner(appletBanner));
    }

    /**
     * 删除小程序banner
     */
    @PreAuthorize("@ss.hasPermi('system:banner:remove')")
    @Log(title = "小程序banner", businessType = BusinessType.DELETE)
	@DeleteMapping("/{bannerIds}")
    public AjaxResult remove(@PathVariable Long[] bannerIds)
    {
        return toAjax(appletBannerService.deleteAppletBannerByBannerIds(bannerIds));
    }
}
